//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackScholesMertonProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (Cephei::QL::IQuote^ x0, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS) : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
    CQuote^ _Cx0;
    CYieldTermStructure^ _CdividendTS;
    CYieldTermStructure^ _CriskFreeTS;
    CBlackVolTermStructure^ _CblackVolTS;
    try
    {
#ifdef HANDLE
        _phBlackScholesMertonProcess = NULL;
#endif
        _Cx0 = safe_cast<CQuote^> (x0);
        _Cx0->Lock();
        Handle<QuantLib::Quote>& _x0 = static_cast<Handle<QuantLib::Quote>&> (_Cx0->GetHandle ()); 
        _CdividendTS = safe_cast<CYieldTermStructure^> (dividendTS);
        _CdividendTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _dividendTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdividendTS->GetHandle ()); 
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CblackVolTS = safe_cast<CBlackVolTermStructure^> (blackVolTS);
        _CblackVolTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _blackVolTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CblackVolTS->GetHandle ()); 
        _ppBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::BlackScholesMertonProcess> (new QuantLib::BlackScholesMertonProcess ( _x0,  _dividendTS,  _riskFreeTS,  _blackVolTS ));
        SetGeneralizedBlackScholesProcess (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx0 != nullptr) _Cx0->Unlock();
        if (_CdividendTS != nullptr) _CdividendTS->Unlock();
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CblackVolTS != nullptr) _CblackVolTS->Unlock();
    }
}
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (boost::shared_ptr<QuantLib::BlackScholesMertonProcess>& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phBlackScholesMertonProcess = NULL;
#endif
	_ppBlackScholesMertonProcess = &childNative;
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
}
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (QuantLib::BlackScholesMertonProcess& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phBlackScholesMertonProcess = NULL;
#endif
	_ppBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::BlackScholesMertonProcess> (&childNative);
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
    _BlackScholesMertonProcessOwner = owner;
    _GeneralizedBlackScholesProcessOwner = owner;
}

Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (CBlackScholesMertonProcess^ copy) : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phBlackScholesMertonProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::BlackScholesMertonProcess> (copy->GetShared());
        _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
    }
}
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (PLATFORM::Type^ t) : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phBlackScholesMertonProcess = NULL;
#endif
	if (!t->IsSubclassOf(CBlackScholesMertonProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (QuantLib::Handle<QuantLib::BlackScholesMertonProcess>& childNative, Object^ owner)  : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
	_phBlackScholesMertonProcess = &childNative;
	_ppBlackScholesMertonProcess = &static_cast<boost::shared_ptr<QuantLib::BlackScholesMertonProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
    _BlackScholesMertonProcessOwner = owner;
}
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (QuantLib::Handle<QuantLib::BlackScholesMertonProcess> childNative)  : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
	_phBlackScholesMertonProcess = &childNative;
	_ppBlackScholesMertonProcess = &static_cast<boost::shared_ptr<QuantLib::BlackScholesMertonProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CBlackScholesMertonProcess::CBlackScholesMertonProcess (QuantLib::BlackScholesMertonProcess childNative)  : CGeneralizedBlackScholesProcess(CBlackScholesMertonProcess::typeid)
{
#ifdef HANDLE
	_phBlackScholesMertonProcess = NULL;
#endif
	_ppBlackScholesMertonProcess = new boost::shared_ptr<QuantLib::BlackScholesMertonProcess> (new QuantLib::BlackScholesMertonProcess (childNative));
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackScholesMertonProcess));
}
#endif

Cephei::QL::Processes::CBlackScholesMertonProcess::~CBlackScholesMertonProcess ()
{
    if (_ppBlackScholesMertonProcess != NULL)
    {
	    delete _ppBlackScholesMertonProcess;
        _ppBlackScholesMertonProcess = NULL;
    }
}
Cephei::QL::Processes::CBlackScholesMertonProcess::!CBlackScholesMertonProcess ()
{
    if (_ppBlackScholesMertonProcess != NULL)
    {
	    delete _ppBlackScholesMertonProcess;
    }
}
QuantLib::BlackScholesMertonProcess& Cephei::QL::Processes::CBlackScholesMertonProcess::GetReference ()
{
    if (_ppBlackScholesMertonProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackScholesMertonProcess;
}
boost::shared_ptr<QuantLib::BlackScholesMertonProcess>& Cephei::QL::Processes::CBlackScholesMertonProcess::GetShared ()
{
    if (_ppBlackScholesMertonProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackScholesMertonProcess;
}
QuantLib::BlackScholesMertonProcess* Cephei::QL::Processes::CBlackScholesMertonProcess::GetPointer ()
{
    if (_ppBlackScholesMertonProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackScholesMertonProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackScholesMertonProcess>& Cephei::QL::Processes::CBlackScholesMertonProcess::GetHandle ()
{
	if (_phBlackScholesMertonProcess == NULL)
	{
		_phBlackScholesMertonProcess = new Handle<QuantLib::BlackScholesMertonProcess> (*_ppBlackScholesMertonProcess);
	}
	return *_phBlackScholesMertonProcess;
}
#endif
bool Cephei::QL::Processes::CBlackScholesMertonProcess::HasNative () 
{
	return (_ppBlackScholesMertonProcess != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IBlackScholesMertonProcess^ Cephei::QL::Processes::CBlackScholesMertonProcess_Factory::Create (Cephei::QL::IQuote^ x0, Cephei::QL::Termstructures::IYieldTermStructure^ dividendTS, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS)
{
    return REFNEW CBlackScholesMertonProcess ( x0,  dividendTS,  riskFreeTS,  blackVolTS);
}
